const util = require('../../utils/util');
const MaterialService = require('../../service/admin/basic_material.service');

async function createMaterial(ctx) {
  const { userId } = util.decodeToken(ctx);
  const {
    code = null,
    partNum = null,
    name = null,
    state = null,
    category_id = null,
    spec = null,
    unit = null,
    brand = null,
    safety_stock = null,
    remark = null,
    project = null,
    shader = null,
    thick = null,
    width = null,
    lengths = null,
    stride = null,
    weight = null,
    consumption = null,
    outNum = null,
    category_name = null,
    type = null,
    url = null
  } = ctx.request.body;

  if (!name) {
    return util.fail(ctx, '物料名称不能为空');
  }

  try {
    const result = await MaterialService.createMaterial({
      code,
      partNum,
      name,
      state,
      category_id,
      category_name,
      spec,
      unit,
      brand,
      safety_stock,
      remark,
      project,
      shader,
      thick,
      width,
      lengths,
      stride,
      weight,
      consumption,
      outNum,
      create_id: userId,
      updata_id: userId,
      type,
      url
    });
    util.success(ctx, { id: result.id });
  } catch (error) {
    console.error('Create material error:', error.message);
    util.fail(ctx, error.message || '创建失败');
  }
}

async function deleteMaterials(ctx) {
  const { ids } = ctx.request.body;

  if (!Array.isArray(ids) || ids.length === 0) {
    return util.fail(ctx, '物料 ID 列表无效');
  }

  try {
    await MaterialService.deleteMaterials(ids);
    util.success(ctx, '');
  } catch (error) {
    console.error('Delete materials error:', error.message);
    util.fail(ctx, error.message || '删除失败');
  }
}

async function updateMaterial(ctx) {
  const { userId } = util.decodeToken(ctx);
  const {
    id,
    code = null,
    partNum = null,
    name = null,
    state = null,
    category_id = null,
    category_name = null,
    spec = null,
    unit = null,
    brand = null,
    safety_stock = null,
    remark = null,
    project = null,
    shader = null,
    thick = null,
    width = null,
    lengths = null,
    stride = null,
    weight = null,
    consumption = null,
    outNum = null,
    url = null
  } = ctx.request.body;

  if (!id || !name) {
    return util.fail(ctx, '物料 ID 或名称不能为空');
  }

  try {
    await MaterialService.updateMaterial({
      id,
      code,
      partNum,
      name,
      state,
      category_id,
      category_name,
      spec,
      unit,
      brand,
      safety_stock,
      remark,
      project,
      shader,
      thick,
      width,
      lengths,
      stride,
      weight,
      consumption,
      outNum,
      updata_id: userId,
      url
    });
    util.success(ctx);
  } catch (error) {
    console.error('Update material error:', error.message);
    util.fail(ctx, error.message || '更新失败');
  }
}

async function getMaterialList(ctx) {
  const { page = 1, limit = 10, code, partNum, name, category_id, project, shader, brand, state, type } = ctx.query;

  try {
    const result = await MaterialService.getMaterialList({
      page: parseInt(page),
      limit: parseInt(limit),
      code,
      partNum,
      name,
      category_id: category_id ? parseInt(category_id) : undefined,
      project,
      shader,
      brand,
      state,
      type
    });
    util.success(ctx, result);
  } catch (error) {
    console.error('Get material list error:', error.message);
    util.fail(ctx, error.message || '查询失败');
  }
}

async function updateMaterialState(ctx) {
  const { userId } = util.decodeToken(ctx);
  const { id, state } = ctx.request.body;

  if (!id) {
    return util.fail(ctx, '物料 ID 无效');
  }
  const newState = state === 1017001 ? 1017002 : 1017001;

  try {
    await MaterialService.updateMaterialState({ id, newState, updata_id: userId });
    util.success(ctx, '');
  } catch (error) {
    console.error('Update material state error:', error.message);
    util.fail(ctx, error.message || '状态更新失败');
  }
}

async function getMaterialInfo(ctx) {
  const { id } = ctx.query;

  if (!id || isNaN(parseInt(id))) {
    return util.fail(ctx, '物料 ID 无效');
  }

  try {
    const result = await MaterialService.getMaterialInfo(parseInt(id));
    util.success(ctx, result);
  } catch (error) {
    console.error('Get material info error:', error.message);
    util.fail(ctx, error.message || '查询失败');
  }
}

module.exports = {
  createMaterial,
  deleteMaterials,
  updateMaterial,
  getMaterialList,
  updateMaterialState,
  getMaterialInfo
};
